package com.hpeng.daily.action;


import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.model.Department;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.vo.Lookup;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("assetCardActionWarehouse")
@Scope("prototype")
public class AssetCardActionWarehouse extends ActionSupport {
	private static final long serialVersionUID = 1L;
	// 用于分页显示
	private int page = 1;
	public String pageLink;
	private Pagination<AssetCard> pagination;
	@Resource
	private AssetCardManager assetCardManager;
	@Resource
	private LoginUserMgr loginUser;
	private List<Department> departments;

	private AssetCard assetCard;
	private String idArray; // 获取被选择的Id编号数组
	private String assetCardQuantity; // 获取录入的资产卡片数量.由编号自增长.其它属性不变
	private String msg;
	private String catid;	//类别编号
	private String acid;	//物品编号

	private List<Lookup> ls;

	private String zhuangTai;	//资产状态
	
	//用于根据权限生成功能按钮
	@Resource
	private Function function;
	private String accessRight;
	private String FN;
	
	public void setAssetCardManager(AssetCardManager assetCardManager) {
		this.assetCardManager = assetCardManager;
	}

	public AssetCard getAssetCard() {
		return assetCard;
	}

	public void setAssetCard(AssetCard assetCard) {
		this.assetCard = assetCard;
	}

	public Pagination<AssetCard> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<AssetCard> pagination) {
		this.pagination = pagination;
	}

	public void setAssetCardManage(AssetCardManager assetCardManager) {
		this.assetCardManager = assetCardManager;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage() {
		return page;
	}

	public void setIdArray(String idArray) {
		this.idArray = idArray;
	}

	public String getIdArray() {
		return idArray;
	}

	public void setAssetCardQuantity(String assetCardQuantity) {
		this.assetCardQuantity = assetCardQuantity;
	}

	public String getAssetCardQuantity() {
		return assetCardQuantity;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getCatid() {
		return catid;
	}

	public void setCatid(String catid) {
		this.catid = catid;
	}
	public String getAccessRight() {
		return accessRight;
	}

	public void setAccessRight(String accessRight) {
		this.accessRight = accessRight;
	}

	public String getFN() {
		return FN;
	}

	public void setFN(String fN) {
		FN = fN;
	}
	public HttpServletRequest request(){
		return ServletActionContext.getRequest();
	}
	public Map<String,Object> session(){
		return ActionContext.getContext().getSession();
	}

	public String getAcid() {
		return acid;
	}

	public void setAcid(String acid) {
		this.acid = acid;
	}
	public List<Lookup> getLs() {
		return ls;
	}

	public void setLs(List<Lookup> ls) {
		this.ls = ls;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}
	
	public String getZhuangTai() {
		return zhuangTai;
	}

	public void setZhuangTai(String zhuangTai) {
		this.zhuangTai = zhuangTai;
	}

	//得到已进入入库阶段的资产信息
	public String getAllWarehonseAssetCard(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumberStr=request.getParameter("page");
		if(pageNumberStr!=null){
			page=Integer.parseInt(pageNumberStr);
		}
		String[] hql = {" from AssetCard where acstatus='131' or acstatus like '2%'"};
		String order = " order by acid asc ";
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("getAllWarehonseAssetCard.action?FN=" + FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code", "status_desc||' - '||sub_desc");
		this.accessRight = function.getAccessRight(session(), FN);
		return "success";
	}
	
	//根据资产编号查询资产的明细值
	public String getAssetCardById(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String acid=request.getParameter("acid");
		if(!"".equals(acid)){
			assetCard=assetCardManager.findAssetCard(acid);
			zhuangTai=function.getStatusDesc(assetCard.getAcstatus().substring(0, 1), assetCard.getAcstatus().substring(1, assetCard.getAcstatus().length()));
		}
		return "success";
	}
	
	//采购物资部经办可以批量确认
	public String updateAssetCardValidate(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acidAll=request.getParameter("acidAll");
			if(!"".equals(acidAll)){
				String[] dids =acidAll.split(",");
				for(String id : dids){
					assetCard=assetCardManager.findAssetCard(id);
					//经办确认的只能是状态处于申报局领已审核的资产
					if("131".equals(assetCard.getAcstatus())){
						assetCard.setAcstatus("201");
						assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("2", "01"));
					}else{
						msg=getText("经办确认的只能是状态处于申报局领已审核的资产");
					}
				}
			}
		} catch (Exception rx) {
			msg = getText("确认失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("确认成功");
		}
		return getAllWarehonseAssetCard();
	}
	
	//采购物资部经办可以批量上报
	public String updateAssetCardReport(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acidAll=request.getParameter("acidAll");
			if(!"".equals(acidAll)){
				String[] dids =acidAll.split(",");
				for(String id : dids){
					assetCard=assetCardManager.findAssetCard(id);
					//经办上报只能上报处于经办已确认的资产
					if("201".equals(assetCard.getAcstatus())){
						assetCard.setAcstatus("202");
						assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("2", "02"));
					}else{
						msg=getText("经办上报只能上报处于经办已确认的资产");
					}
				}
			}
		} catch (Exception rx) {
			msg = getText("上报失败，请重新操作");
			
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("上报成功");
		}
		return getAllWarehonseAssetCard();
	}
	
	//采购物资部领导只能单个审核操作
	public String updateAssetCardDeclaration(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acid=request.getParameter("acid");
			String ideaValue=new String(request.getParameter("ideaValue").getBytes("iso8859-1"),"UTF-8");
			if(!"".equals(acid)){
				assetCard=assetCardManager.findAssetCard(acid);
				//判断只能审核经办已上报的资产
				if("202".equals(assetCard.getAcstatus())){
					assetCard.setAcstatus("203");
					assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),ideaValue,function.getStatusDesc("2", "03"));
				}else{
					msg=getText("只能审核经办已上报的资产");
				}
			}
		} catch (Exception rx) {
			msg = getText("审核失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("审核成功");
		}
		return getAllWarehonseAssetCard();
	}
	
	//采购物资部领导只能单个退回操作
	public String updateAssetCardUntread(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acid=request.getParameter("acid");
			String ideaValue=new String(request.getParameter("ideaValue").getBytes("iso8859-1"),"UTF-8");
			if(!"".equals(acid)){
				assetCard=assetCardManager.findAssetCard(acid);
				//判断只能退回状态处于经办已上报的资产
				if("202".equals(assetCard.getAcstatus())){
					assetCard.setAcstatus("201");
					assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),ideaValue,function.getStatusDesc("2", "01"));
				}else{
					msg=getText("只能退回状态处于经办已上报的资产");
				}
			}
		} catch (Exception rx) {
			msg = getText("退回失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("退回成功");
		}
		return getAllWarehonseAssetCard();
	}
	
	//条件查询
	public String getWarehonseAssetCardByPageNumberAndValue(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String propertyValue=request.getParameter("propertyValue");
		String pageNumberStr=request.getParameter("page");
		if(pageNumberStr!=null){
			page=Integer.parseInt(pageNumberStr);
		}
		String[] hql=new String[1];
		if("acstatus".equals(propertyValue)){
			String propertyValueBeChoose=request.getParameter("propertyValueBeChoose"); 
			System.out.println("propertyValueBeChoose--->"+propertyValueBeChoose);
			hql[0] = " from AssetCard where acstatus='131' and "+propertyValue+" like '%"+propertyValueBeChoose+"%' or acstatus like '2%' and "+propertyValue+" like '%"+propertyValueBeChoose+"%'";
			String order = " order by acid asc ";
			setPagination(assetCardManager.getPagination(page, hql, order));
			pageLink = pagination.getPageLink("getWarehonseAssetCardByPageNumberAndValue.action?FN=" + FN+"&propertyValue="+propertyValue+"&propertyValueBeChoose="+propertyValueBeChoose);
		}else{
			String inputValue;
			try {
				inputValue = new String(request.getParameter("inputValue").getBytes("iso8859-1"),"UTF-8");
				System.out.println("inputValue--->"+inputValue);
				hql[0] = " from AssetCard where acstatus='131' and "+propertyValue+" like '%"+inputValue+"%' or acstatus like '2%' and "+propertyValue+" like '%"+inputValue+"%'";
				String order = " order by acid asc ";
				setPagination(assetCardManager.getPagination(page, hql, order));
				pageLink = pagination.getPageLink("getWarehonseAssetCardByPageNumberAndValue.action?FN=" + FN+"&propertyValue="+propertyValue+"&inputValue="+inputValue);
			} catch (Exception rx) {
				msg = getText("条件查询失败，请重新操作");
			}
		}
		ls = function.getLookup("prod_status_mst", "status_code||sub_code", "status_desc||' - '||sub_desc");
		this.accessRight = function.getAccessRight(session(), FN);
		
		return "success";
	}
	
	// 点击类别树上的类别得到该类别下的处于入库状态的资产
	public String queryAssetInTree() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String catid=request.getParameter("catid");
		int page=1;
		if(request.getParameter("page")!=null){
			page=Integer.parseInt(request.getParameter("page"));
		}
		String[] hql = { " from AssetCard where acstatus='131' and catid like '"+catid+"%' or acstatus like '2%' and catid like '"+catid+"%'" };
		String order = " ";
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("queryAssetInTree.action?FN=" + FN+"&catid="+catid);
		
		ls = function.getLookup("prod_status_mst", "status_code||sub_code", "status_desc||' - '||sub_desc");
		this.accessRight = function.getAccessRight(session(), FN);
		return "success";
	}
	
}
